package Dao;

import java.sql.*;

public class UntilHive {
    private static String driverName = "org.apache.hive.jdbc.HiveDriver";
    private static String database = "default";
    private static String url = "jdbc:hive2://master:10000/";
    private static String auth ="noSasl";
    private static String user = "hive";
    private static String pwd = "";

    static private Connection con = null;

    static {
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    public static Connection getCon(String db) {
        try {
            con = DriverManager.getConnection(url+db, user, pwd);
            //con = DriverManager.getConnection(url+db+";auth="+auth, user, pwd);//NOSASL
        } catch (SQLException e) {
            System.out.println("None连接模式运行出错，现在尝试NOSASL连接模式...");
            try {
                con = DriverManager.getConnection(url+db+";auth="+auth, user, pwd);//NOSASL
            } catch (SQLException e1) {
                e.printStackTrace();
                e1.printStackTrace();
            }
        }
        return con;
    }

    public static Connection getCon() {
        try {
           // con = DriverManager.getConnection(url+database, user, pwd);
            con = DriverManager.getConnection(url+database+";auth="+auth, user, pwd);//NOSASL
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public static boolean closeAll(Connection connection, Statement statement, ResultSet resultSet){
        boolean over = true;
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
                over = false;
            }
        }
        if (statement != null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
                over = false;
            }
        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
                over = false;
            }
        }
        return over;
    }
}
